import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_application_1/provider/location_provider.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_picker/PickerLocalizationsDelegate.dart';
import 'package:provider/provider.dart';
import 'components/hide_scroll_shadow/hide_scroll_shadow.dart';
import 'package:flutter_application_1/utils/utils.dart';
// 初始页
import 'pages/splash.dart';
// 全局
import 'package:flutter_application_1/global.dart';
// 路由
import 'package:flutter_application_1/router/observer.dart';
import 'package:flutter_application_1/router/routes.dart';
// 状态管理
import 'package:flutter_application_1/provider/provider.dart';
// 下拉刷新
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:flutter_application_1/components/refresh-helper/refresh_config.dart';
// 下载文件插件
import 'package:flutter_downloader/flutter_downloader.dart';
// 常用工具
import 'package:flustars/flustars.dart';
// 推送
import 'package:flutter_application_1/plugins/push.dart';
// 定位
import 'package:amap_flutter_location/amap_flutter_location.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // 以下两行 设置android状态栏为透明的沉浸。写在组件渲染之后，是为了在渲染后进行set赋值，覆盖状态栏，写在渲染之前MaterialApp组件会覆盖掉这个值。
  if (Platform.isAndroid) {
    SystemUiOverlayStyle systemUiOverlayStyle =
        SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }

  // 强制竖屏
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitDown,
    DeviceOrientation.portraitUp,
  ]).then((_) {
    Utils.statusBarEnable(true);

    // 路由
    RouterUtils routerUtils = RouterUtils();
    Global.routeMap = Routes.configureRoutes(routerUtils.getRouter());
    Global.routerUtils = routerUtils;

    runApp(
      AppProvider(
        child: App(),
      ),
    );
  });
}

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {
  @override
  void initState() {
    super.initState();
    _initAsync();
  }

  // 异步初始化
  void _initAsync() async {
    // 等待sp初始化完成后再运行app
    SpUtil.getInstance();
    // 初始化下载文件插件
    FlutterDownloader.initialize();
    // 推送
    Push.init();
    // 渲染后
    WidgetsBinding.instance!.addPostFrameCallback((_) {
      // 初始化定位
      Provider.of<LocationProvider>(context, listen: false).init(context);
    });
  }

  @override
  Widget build(BuildContext context) {
    return AppRefreshConfig(
      child: AnnotatedRegion<SystemUiOverlayStyle>(
        value: SystemUiOverlayStyle(
          statusBarBrightness: Brightness.dark,
          statusBarIconBrightness: Brightness.dark,
          systemNavigationBarIconBrightness: Brightness.dark,
          statusBarColor: Colors.transparent,
        ),
        child: MaterialApp(
          // 消除滚动的月牙阴影
          home: HideScrollShadow(
            // 打开的初始页面
            child: Splash(),
          ),
          localizationsDelegates: [
            // 下拉刷新
            RefreshLocalizations.delegate,
            // 解决 Cupertino 弹框报错的问题
            GlobalCupertinoLocalizations.delegate,
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            // flutter_picker
            PickerLocalizationsDelegate.delegate,
          ],
          locale: Locale('zh-CN'),
          // locale: Locale('en'),
          supportedLocales: [
            const Locale('zh', 'CN'),
            const Locale('en', 'US'),
          ],
          navigatorObservers: [
            GlobalObserver(),
          ],
        ),
      ),
    );
  }
}
